package com.franklin.java.configuration.sql.type;

import com.franklin.java.api.configuration.AbstractProjectConfigurable;
import com.franklin.java.configuration.sql.type.ui.TypeMappingComponent;
import com.franklin.java.entity.group.TypeMappingGroup;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Map;

/**
 * @author Franklin
 * @since 2021/2/7 14:55
 */
public class TypeMappingConfigurable extends AbstractProjectConfigurable {

    private TypeMappingComponent typeMappingComponent;

    public TypeMappingConfigurable(Project myProject) {
        super(myProject);
    }

    @Nls
    @Override
    public String getDisplayName() {
        return "Type Mapping Settings";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        TypeMappingState typeMappingState = TypeMappingState.getInstance();
        typeMappingComponent = new TypeMappingComponent(SelectGroupState.getSelectGroup(myProject),typeMappingState.groupNameTypeMappingGroupMap);
        return typeMappingComponent.getMainPanel();
    }

    @Override
    public boolean isModified() {
        TypeMappingState typeMappingState = TypeMappingState.getInstance();
        if (!typeMappingComponent.getCurGroupName().equals(SelectGroupState.getSelectGroup(myProject))){
            return true;
        }

        Map<String, TypeMappingGroup> groupNameMappingGroupMapNow = typeMappingComponent.getGroupNameMappingGroupMap();
        return groupNameMappingGroupMapNow.equals(typeMappingState.groupNameTypeMappingGroupMap);
    }

    @Override
    public void apply() throws ConfigurationException {
        TypeMappingState typeMappingState = TypeMappingState.getInstance();
        SelectGroupState.getInstance(myProject).selectGroup = typeMappingComponent.getCurGroupName();
        typeMappingState.groupNameTypeMappingGroupMap = typeMappingComponent.getGroupNameMappingGroupMap();
    }

}
